#include "blur_gaussian.h"
#include <cmath>

BlurGaussian::BlurGaussian(PNM* img) :
    Convolution(img)
{
}

BlurGaussian::BlurGaussian(PNM* img, ImageViewer* iv) :
    Convolution(img, iv)
{
}

PNM* BlurGaussian::transform()
{
    emit message("Blurring...");

    int size = getParameter("size").toInt();
    radius = (size/2)+1;
    sigma = getParameter("sigma").toDouble();

    return convolute(getMask(size, Normalize), RepeatEdge);
}

PNM* BlurGaussian::transformForParameters(int _size, double _sigma)
{
    emit message("Blurring...");

    int size = _size;
    radius = (size/2)+1;
    sigma = _sigma;

    return convolute(getMask(size, Normalize), RepeatEdge);
}

math::matrix<double> BlurGaussian::getMask(int size, Mode)
{
    math::matrix<double> mask(size, size);

	int r = size / 2;
	for (int i = -r; i <= r; i++)
	{
		for (int j = -r; j < r; j++)
		{
			mask(i + r, j + r) = getGauss(i, j, sigma);
		}
	}

    return mask;
}

double BlurGaussian::getGauss(int x, int y, double sigma)
{
	double factor = 1 / (2* 3.1415* std::pow(sigma, 2));
	double res_e = std::exp( -(std::pow(x, 2) + std::pow(y, 2))/(2.0 * std::pow(sigma, 2))    );

	return factor * res_e;
}

